//
//  UpgradeAlert.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/8/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
typealias down = () ->()
typealias cancel = () ->()
class UpgradeAlert: UIView {
    var titleStr = ""
    var verson = ""
    var downBlock:down?
    var cancelBlock:cancel?
    var isUpgrade = false
    init(describe:String,verson:String,isUpgradeBool:Bool = false) {
        super.init(frame:CGRect.zero)
        titleStr = describe
        self.verson = verson
        self.isUpgrade = isUpgradeBool
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() {
        let backView = self
        backView.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        backView.frame = CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: kSCREEN_HEIGHT)
        UIApplication.shared.keyWindow?.addSubview(backView)
        
        let aletView = UIView()
        aletView.layer.cornerRadius = 5
        aletView.backgroundColor = UIColor.white
        backView.addSubview(aletView)
        aletView.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(48)
            make.right.equalToSuperview().offset(-48)
            make.height.equalTo(300)
        }
        
        let imageView = UIImageView(image: UIImage(named: "downLoadImage"))
        aletView.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(130)
        }
        
        let nameLabel = UILabel()
        nameLabel.textColor = UIColor(hexStr: "#373b58")
        nameLabel.text = String(format: "V %@ %@", verson,kText("版本更新"))
        nameLabel.font = UIFont.boldSystemFont(ofSize: 14)
        nameLabel.textAlignment = NSTextAlignment.center
        aletView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalToSuperview().offset(148)
        }
        
        let infoLabel = UILabel()
        infoLabel.font = UIFont.systemFont(ofSize: 14)
        infoLabel.textColor = UIColor(hexStr: "#41465a")
        infoLabel.text = titleStr
        infoLabel.textAlignment = NSTextAlignment.center
        aletView.addSubview(infoLabel)
    
        infoLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(180)
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
        }
        
        let downloadBtn  = UIButton()
        downloadBtn.layer.cornerRadius = 35/2
        downloadBtn.addTarget(self, action: #selector(downloadBtnAction), for: UIControlEvents.touchUpInside)
        downloadBtn.backgroundColor = kThemeColor
        downloadBtn.setTitle(kText("去下载"), for: UIControlState.normal)
        downloadBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        downloadBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
        aletView.addSubview(downloadBtn)
        downloadBtn.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(240)
            make.left.equalToSuperview().offset(20)
            make.right.equalToSuperview().offset(-20)
            make.height.equalTo(35)
        }
        
        let cancelBtn = UIButton()
        cancelBtn.isHidden = isUpgrade
        cancelBtn.addTarget(self, action: #selector(cancelBtnAction), for: UIControlEvents.touchUpInside)
        cancelBtn.setImage(UIImage(named: "downCancel"), for: UIControlState.normal)
        backView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { (make) in
            make.bottom.equalTo(aletView.snp.top).offset(-15)
            make.right.equalTo(aletView)
            make.height.width.equalTo(30)
            
        }
        
       
    }
    
    @objc func downloadBtnAction() {
        if downBlock != nil {
            downBlock!()
        }
        self.removeFromSuperview()
    }
    
    @objc func cancelBtnAction() {
        if cancelBlock != nil {
            cancelBlock!()
        }
        self.removeFromSuperview()
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
